package com.huzp.net.mysql.jdbc;

import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ConnectionInfo {

    private static Pattern URL_PATTERN = Pattern.compile("jdbc:mysql://(.*):(\\d)/([a-z]|[A-Z]|\\d)?(.*=.*)*");
    private static Pattern HOST_PATTERN = Pattern.compile("//(.*):(\\d*)/([a-z]|[A-Z]|\\d)*");

    private HostInfo hostInfo;

    private boolean useUnicode;

    public ConnectionInfo(String url, Properties info) {
        String username = (String) info.get("user");
        String password = (String) info.get("password");
        Matcher matcher = HOST_PATTERN.matcher(url);
        if (matcher.find()) {
            String hostStr = matcher.group().substring(2);
            String host = hostStr.substring(0, hostStr.indexOf(":"));
            Integer port = Integer.parseInt(hostStr.substring(hostStr.indexOf(":") + 1, hostStr.indexOf("/")));
            String dbName = hostStr.substring(hostStr.indexOf("/") + 1);
            this.hostInfo = new HostInfo(host, port, dbName, username, password);
        }

    }

    public HostInfo getHostInfo() {
        return hostInfo;
    }
}
